package com.bright.ghj.common.util;

import com.bright.ghj.common.pojo.query.PageQuery;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * @Author txf
 * @Date 2022/12/10 17:51
 * @Description 分页参数转换工具类
 */
public abstract class PageUtil {

    public static Pageable toPageable(PageQuery pageQuery){
//        if(!(query instanceof PageQuery)) {
//            return null;
//        }
//        PageQuery pageQuery = (PageQuery) query;
        Pageable pageable;
        if(CollectionUtil.isEmpty(pageQuery.getSorts())){
            pageable = PageRequest.of(pageQuery.getPageNumber() - 1, pageQuery.getPageSize());
        }else {
            pageable = PageRequest.of(pageQuery.getPageNumber() - 1, pageQuery.getPageSize(), SortUtil.toSort(pageQuery.getSorts()));
        }
        return pageable;
    }

    public static Pageable toPageable(Integer pageNumber, Integer pageSize, List<String> sorts){
        Pageable pageable = null;
        if(CollectionUtil.isEmpty(sorts)){
            pageable = PageRequest.of(pageNumber, pageSize);
        }else {
            pageable = PageRequest.of(pageNumber, pageSize, SortUtil.toSort(sorts));
        }
        return pageable;
    }
}
